<?php

namespace App\Http\Admin\Controllers;

use App\Api\Exceptions\BusinessException;
use App\Http\Admin\Services\AdminPermissionService;
use App\Http\Controllers\Controller;

class AdminPermissionController extends Controller
{
    public function __construct(public \App\Api\Contracts\ApiResponseContract $api, public \App\Api\Contracts\VerifyReqContract $ver, public AdminPermissionService $permission_service)
    {
    }


    /**
     * 获取权限列表
     * @return mixed
     */
    public function list(): mixed
    {
        $params = $this->ver->checkParams([], ['offset:int', 'limit:int']);
        return $this->api->success(datas: $this->permission_service->listPermissionTree($params));
    }

    /**
     * 添加权限
     * @return mixed
     */
    public function add(): mixed
    {
        $params = $this->ver->checkParams(['name:string', 'type:in:0,1,2', 'title:string'], ['parent_id:int', 'router', 'icon', 'order_num:int', 'view_path', 'keep_alive:in:0,1', 'is_show:in:0,1']);
        $result = $this->permission_service->addPermission($params);
        return $this->api->success(message: '更新成功', datas: $result);
    }

    /**
     * 修改权限
     * @return mixed
     */
    public function update(): mixed
    {
        $params = $this->ver->checkParams(['id:int'], ['name:string', 'parent_id:int', 'router', 'icon', 'order_num:int', 'view_path', 'keep_alive:in:0,1', 'is_show:in:0,1', 'title:string']);
        $result = $this->permission_service->updatePermission($params);
        return $this->api->success(message: '更新成功', datas: $result);
    }

    /**
     * 删除权限
     * @return mixed
     * @throws BusinessException
     */
    public function delete(): mixed
    {
        $params = $this->ver->checkParams(['id:int']);
        $this->permission_service->deletePermission($params['id']);

        return $this->api->success('删除成功');
    }

    /**
     * 指定ID权限详情
     * @return mixed
     */
    public function info(): mixed
    {
        $params = $this->ver->checkParams(['id:int']);
        return $this->api->success(datas: $this->permission_service->permissionInfo($params['id']));
    }

    public function getTypePermissions()
    {
        $params = $this->ver->checkParams([], ['permission_type:string']);
        return $this->api->success(datas: $this->permission_service->getTypePermissions($params));
    }

}
